#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    long long maxTotal(vector<int> &value, vector<int> &limit)
    {
        int n = value.size();
        vector<vector<int>> groups(n + 1);
        for (int i = 0; i < n; ++i)
        {
            groups[limit[i]].push_back(value[i]);
        }
        long long ans = 0;
        for (int l = 1; l <= n; ++l)
        {
            priority_queue<int> pq(groups[l].begin(), groups[l].end());
            int cnt = 0;
            if (!pq.empty() && cnt < l)
            {
                ans += pq.top();
                pq.pop();
            }
        }
        return ans;
    }
};
